{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cb021d88",
   "metadata": {},
   "source": [
    "# week06\n",
    "## 一.列表的基本方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "39800068",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "# 思考，如何取出所有位置10001的index？\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04570e86",
   "metadata": {},
   "source": [
    "### 1.增加的方法\n",
    "### 1.1 增加 append()  增加在list的末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dd7b63b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 1]\n"
     ]
    }
   ],
   "source": [
    "names.append(1)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3d2ce45",
   "metadata": {},
   "source": [
    "### 1.2 插入 insert() 可以指定位置增加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6cf6988e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "insert之后的names： ['Mike', 'zengqiaoyi', 'yougege', 'Mary', 'Jan', 'Jack', 1]\n"
     ]
    }
   ],
   "source": [
    "names.insert(1,'zengqiaoyi')\n",
    "print(\"insert之后的names：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94d74738",
   "metadata": {},
   "source": [
    "### 2. 删除 remove  和 pop\n",
    "### 优势：删除list中多个元素时，用remove比较方便合适"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4b276d81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nRemove first occurrence of value.\\nRaises ValueError if the value is not present.\\n'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Remove first occurrence of value.\n",
    "Raises ValueError if the value is not present.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02cc0d3c",
   "metadata": {},
   "source": [
    "### 2.1 remove()一般情况下 使用remove先用in做判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e6f2efde",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove删除的结果： ['Mike', 'zengqiaoyi', 'yougege', 'Mary', 'Jan', 'Jack', 1]\n"
     ]
    }
   ],
   "source": [
    "if 10001 in names:\n",
    "    names.remove(10001)\n",
    "print(\"remove删除的结果：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3751a621",
   "metadata": {},
   "source": [
    "### 2.2 pop() 删除位置为1的这个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6edd07a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pop()方法后的names: ['Mike', 'zengqiaoyi', 'yougege', 'Mary', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "names.pop(-1)\n",
    "print(\"pop()方法后的names:\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a110de72",
   "metadata": {},
   "source": [
    "### 3. index()索引 查看该元素如10001 在stu_id中第一个出现的位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e837f35f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "print(stu_id.index(10001))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1995282b",
   "metadata": {},
   "source": [
    "### 4. clear()清除所有数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "422c4ae4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除names列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "names.clear()\n",
    "print(\"清除names列表后的值：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4105f4a6",
   "metadata": {},
   "source": [
    "### 5.count()查看列表元素出现的个数，（简单应用）统计分数出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "be5bad47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count方法的结果: 3\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "print(\"count方法的结果:\",stu_id.count(10001))\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cd24411",
   "metadata": {},
   "source": [
    "### 6. extend() 是给指定的list进行扩充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9d787e5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ce7c283",
   "metadata": {},
   "source": [
    "### 列表的其他方法(Sequence Types 的一些方法)\n",
    "### 1. len()查看list长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bb5b4674",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "names的长度/names的列表元素个数： 9\n"
     ]
    }
   ],
   "source": [
    "print(\"names的长度/names的列表元素个数：\",len(names))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30768721",
   "metadata": {},
   "source": [
    "### 2. in 查看元素是否存在list当中,结果为布尔值，False/True,\n",
    "###    一般在操作list的修改和删除时会先判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bc9ca2ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(\"zengqiaoyi\" in names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8ebbbf4",
   "metadata": {},
   "source": [
    "## 二.列表的高级方法_枚举"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ca98c309",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "# 如何取出所有位置10001的index？\n",
    "for i in stu_id:\n",
    "    # 循环遍历stu_id的所有内容\n",
    "    if i == 10001:\n",
    "        # 元素为10001的时候,取出索引值\n",
    "        print()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9e7577f",
   "metadata": {},
   "source": [
    "### 枚举:因为list其实不仅有values值，还有index索引，但for循环主要循环遍历其值，不遍历索引\n",
    "### 因此，有了枚举的方法，同时遍历两者\n",
    "#### enumerate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "97247b81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 10001\n",
      "5 10001\n",
      "8 10001\n",
      "我们想要删除pop()的值： [0, 5, 8]\n"
     ]
    }
   ],
   "source": [
    "pop_values_list = []  # append方法经常用于新建列表\n",
    "for index,item in enumerate(stu_id):\n",
    "    # print(index,item)\n",
    "    if item == 10001:\n",
    "        print(index,item)\n",
    "        pop_values_list.append(index)\n",
    "print(\"我们想要删除pop()的值：\",pop_values_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "339ff3ea",
   "metadata": {},
   "source": [
    "#### 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "38ec9694",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_id = [95,97,90,90,97,88,90]\n",
    "# 删除 97 的值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9dc22c0",
   "metadata": {},
   "source": [
    "##### 方法一 : remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3196b8eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[95, 90, 90, 88, 90]\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    if 97 in stu_id:\n",
    "        stu_id.remove(97)\n",
    "    else:\n",
    "        break\n",
    "print(stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fadec05",
   "metadata": {},
   "source": [
    "##### 方法二: pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e43b28c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 95\n",
      "1 90\n",
      "2 90\n",
      "3 88\n",
      "4 90\n",
      "[95, 90, 90, 88, 90]\n"
     ]
    }
   ],
   "source": [
    "# print(stu_id.index(97))\n",
    "for index,item in enumerate(stu_id):\n",
    "    print(index,item)\n",
    "    if item == 97:\n",
    "        print(index)\n",
    "        stu_id.pop(index)\n",
    "print(stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdd03187",
   "metadata": {},
   "source": [
    "## 二.列表的高级方法_copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a4dde649",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a25f405",
   "metadata": {},
   "source": [
    "### copy()很重要\n",
    "### 赋值 不等于 复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8f922e1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1015b2d9",
   "metadata": {},
   "source": [
    "### 1. 赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "efa3139b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10003, 10004, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b255f29",
   "metadata": {},
   "source": [
    "### 2.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9418a5f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10004, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e222441d",
   "metadata": {},
   "source": [
    "### 3.切片实现复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a0ab0ea8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [10001, 10008, 10001, 10009, 10010, 10001] \n",
      " stu_id_matedata: [10001, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dee416c0",
   "metadata": {},
   "source": [
    "### 一些课外补充"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3127684b",
   "metadata": {},
   "source": [
    "### 1.列表可以存储不同的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "d571ab84",
   "metadata": {},
   "outputs": [],
   "source": [
    "# li = [1,1.2,'hello',True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c98c20dd",
   "metadata": {},
   "source": [
    "### 2.列表的特性\n",
    "### 定义一个列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "c8175431",
   "metadata": {},
   "outputs": [],
   "source": [
    "service = ['http','ssh','ftp','dns']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d3ffeec",
   "metadata": {},
   "source": [
    "### （1）索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "dbba00c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http\n",
      "dns\n"
     ]
    }
   ],
   "source": [
    "print(service[0])  ##索引列表的第一个元素\n",
    "print(service[-1]) ##索引列表中的最后一个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d328d45a",
   "metadata": {},
   "source": [
    "### （2）切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "adb27bcf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ssh', 'ftp', 'dns']\n",
      "['http', 'ssh', 'ftp']\n",
      "['dns', 'ftp', 'ssh', 'http']\n"
     ]
    }
   ],
   "source": [
    "print(service[1:])  ##输出列表中除了第一个元素的值\n",
    "print(service[:-1]) ##输出列表中除了最后一个元素的值\n",
    "print(service[::-1]) ##将列表的成员倒序输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9de61dd9",
   "metadata": {},
   "source": [
    "### （3）重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "835895de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['http', 'ssh', 'ftp', 'dns', 'http', 'ssh', 'ftp', 'dns', 'http', 'ssh', 'ftp', 'dns']\n"
     ]
    }
   ],
   "source": [
    "print(service * 3)  ##将列表里的所有成员输出三次"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9da46df",
   "metadata": {},
   "source": [
    "### （4）连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "077df2f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# service = ['mysql','firewalld']\n",
    "# print(service + service1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b0f3be6",
   "metadata": {},
   "source": [
    "### （5）成员操作符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "49771c1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# print('mysql' in service1)\n",
    "# print('mysql' not in serviceq)  ##返回一个布尔值：Ture/False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e45699e2",
   "metadata": {},
   "source": [
    "### （6）迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "3b39909f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# for se in service\n",
    "# print(se)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f43ffc34",
   "metadata": {},
   "source": [
    "### 3.添加列表元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "bb7d54f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "service = ['http','ssh','ftp','dns']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c1df416",
   "metadata": {},
   "source": [
    "### （1）append:追加一个元素到列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "0eb1b557",
   "metadata": {},
   "outputs": [],
   "source": [
    "service.append('firewalld')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c897ef90",
   "metadata": {},
   "source": [
    "### （2）extend:拉伸 追加多个元素到列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "86b09ca6",
   "metadata": {},
   "outputs": [],
   "source": [
    "service.extend(['mysql','firewalld'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc94d5e4",
   "metadata": {},
   "source": [
    "### （3）insert:在指定索引位置插入元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "732bfb97",
   "metadata": {},
   "outputs": [],
   "source": [
    "service.insert(1,'samba')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67aa9f4f",
   "metadata": {},
   "source": [
    "## week07\n",
    "### 列表的基础用法2.0\n",
    "#### 回顾week06："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "bb468096",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_str = \"Alex,Marry,Jack\"\n",
    "names_list = [\"Alex\",\"Marry\",\"Jcak\"]\n",
    "\n",
    "# print(names_str[0:4]) # 字符串，无法确定名字长度 用切片的复杂操作\n",
    "# print(names_list[0]) # 用index索引的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "635fdae4",
   "metadata": {},
   "source": [
    "### list的方法\n",
    "#### 思考：对于一个序列数据，我们可以做什么操作？\n",
    "### 1.取值：切片\n",
    "### 2.增加/新增数据，stu_网新1班 =[...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c0ff82b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f3d5837",
   "metadata": {},
   "source": [
    "### 2.1 append() 圆括号的内容是 对象（比较大的一个范围，python中一切皆是对象）\n",
    "###     append() 新增在末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c6bc4631",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'xiaozeng', 'xiaoyao']\n"
     ]
    }
   ],
   "source": [
    "names.append(\"xiaozeng\")\n",
    "names.append(\"xiaoyao\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "714bf196",
   "metadata": {},
   "source": [
    "### 2.2 insert() 指定位置新增数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a125b38e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'zhichao', 'Jan', 'Jack', 'xiaozeng', 'xiaoyao']\n"
     ]
    }
   ],
   "source": [
    "names.insert(2,\"xiaole\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "391e51c0",
   "metadata": {},
   "source": [
    "### 3.删除\n",
    "### 3.1 remove() 的操作应该先做查询，判断元素value是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0d5ddb87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'zhichao', 'Jan', 'Jack', 'xiaoyao']\n"
     ]
    }
   ],
   "source": [
    "names.remove(\"xiaozeng\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1aed21a4",
   "metadata": {},
   "source": [
    "* 先做查询： seq的一般方法（通用方法）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "5dca00d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(\"lihua\" in names) # False/True\n",
    "# username = input(\"names:\")\n",
    "# if username in names:\n",
    "    # 删除该用户\n",
    "#     names.remove(username)\n",
    "# else:\n",
    "#     names.append(username)\n",
    "# print(names)\n",
    "# names.remove(\"lihua\")\n",
    "# print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebbbf7a8",
   "metadata": {},
   "source": [
    "### 3.2 pop() 在使用pop()之前，要先了解list的长度,清楚删除的索引位置.\n",
    "### 要先了解list的长度 seq 一般方法 len()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "cf8bdae7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "['Mike', 'Mary', 'zhichao', 'Jan', 'Jack', 'xiaoyao']\n",
      "['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao']\n"
     ]
    }
   ],
   "source": [
    "print(len(names))\n",
    "print(names)\n",
    "names.pop(2)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "533f9f13",
   "metadata": {},
   "source": [
    "#### 清楚删除的索引位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "65784c0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(names.index(\"Jan\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "42f639f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例： 删除一些具有特征的索引值\n",
    "# for i in range(0,len(names)-1,2):\n",
    "#     names.pop(i)\n",
    "# print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04534f67",
   "metadata": {},
   "source": [
    "### 列表的高级用法_枚举2.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "fccd49bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_网新1 = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names_网新2 = [\"Alex\",\"xiaozeng\",\"xiaoyao\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89b6bff8",
   "metadata": {},
   "source": [
    "### 4.删除列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "09861153",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "names.clear()\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4db46985",
   "metadata": {},
   "source": [
    "### 5.合并列表 extend 是有顺序的，（）里面的list排在后面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "00507091",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Alex', 'xiaozeng', 'xiaoyao', 'Mike', 'Mary', 'Jan', 'Jack', 'Mike', 'Mary', 'Jan', 'Jack', 'Mike', 'Mary', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "names_网新2.extend(names_网新1)\n",
    "print(names_网新2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70436d27",
   "metadata": {},
   "source": [
    "### 6. copy() 复制列表  赋值 不等于 真正意义的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "5b197e88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack']\n",
      "['Mike', 'Mary', 'Jan', 'Jack']\n",
      "names_网新1: ['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao']\n",
      "names_网新1复制: ['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao']\n"
     ]
    }
   ],
   "source": [
    "names_网新1复制 = names_网新1\n",
    "print(names_网新1)\n",
    "print(names_网新1复制)\n",
    "names_网新1.append(\"xiaoyao\")\n",
    "print(\"names_网新1:\",names_网新1)\n",
    "print(\"names_网新1复制:\",names_网新1复制)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "608ffc77",
   "metadata": {},
   "source": [
    "### 列表的高级用法_copy()2.0\n",
    "### 6. copy() 复制列表  赋值 不等于 真正意义的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "33d59ba6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao']\n",
      "['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao']\n",
      "names_网新1: ['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao']\n",
      "names_网新1复制: ['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao']\n"
     ]
    }
   ],
   "source": [
    "names_网新1复制 = names_网新1\n",
    "print(names_网新1)\n",
    "print(names_网新1复制)\n",
    "names_网新1.append(\"xiaoyao\")\n",
    "print(\"names_网新1:\",names_网新1)\n",
    "print(\"names_网新1复制:\",names_网新1复制)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bd96833",
   "metadata": {},
   "source": [
    "#### 引入copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "6ccbdc53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao']\n",
      "['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao']\n",
      "names_网新1: ['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao', 'xiaoyao']\n",
      "names_网新1_copy: ['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao']\n"
     ]
    }
   ],
   "source": [
    "names_网新1_源数据备份 = names_网新1.copy()\n",
    "print(names_网新1)\n",
    "print(names_网新1_源数据备份)\n",
    "names_网新1.append(\"xiaoyao\")\n",
    "print(\"names_网新1:\",names_网新1)\n",
    "print(\"names_网新1_copy:\",names_网新1_源数据备份)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27a49869",
   "metadata": {},
   "source": [
    "#### 用切片 [:] 等价于 copy()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "cde2b085",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao', 'xiaoyao']\n",
      "['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao', 'xiaoyao']\n",
      "names_网新1: ['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao', 'xiaoyao', 'xiaoyao']\n",
      "names_网新1_源数据备份: ['Mike', 'Mary', 'Jan', 'Jack', 'xiaoyao', 'xiaoyao', 'xiaoyao']\n"
     ]
    }
   ],
   "source": [
    "names_网新1_源数据备份 = names_网新1[:]\n",
    "print(names_网新1)\n",
    "print(names_网新1_源数据备份)\n",
    "names_网新1.append(\"xiaoyao\")\n",
    "print(\"names_网新1:\",names_网新1)\n",
    "print(\"names_网新1_源数据备份:\",names_网新1_源数据备份)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c77fc04",
   "metadata": {},
   "source": [
    "### 7. count() 查看元素出现的次数/统计数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "cb65ee76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "stu_id = [95,97,90,90,97,88,90]\n",
    "print(stu_id.count(90))# 查看90出现的次数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f55adff",
   "metadata": {},
   "source": [
    "### 8. sort() 排序 reverse=True 从高到低，默认从低到高排"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "8568e81f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[97, 97, 95, 90, 90, 90, 88]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [95,97,90,90,97,88,90]\n",
    "stu_id.sort(reverse=True)\n",
    "print(stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efeb091a",
   "metadata": {},
   "source": [
    "### week07 课本练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "522bacf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "plist= ['o', 'n', ' ', 't', 'a', 'p']\n",
      "new_phrase= on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # 把字符串转变为 list ： list(str)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "# pop() 按index索引删除 如果不填则删除末位  对应记忆==>append()增加末位\n",
    "\n",
    "# 循环4次 进行四次plist的pop()方法操作，即删除4次末位\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "print(plist)\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "#                  a     ,      p\n",
    "print(plist)\n",
    "# 删除 位置3的 ‘ ’ 并且插入到  位置 2\n",
    "plist.insert(2,plist.pop(3))\n",
    "print(plist)\n",
    "#\n",
    "new_phrase = ''.join(plist) # 列表元素的合并\n",
    "print(\"plist=\",plist)\n",
    "print(\"new_phrase=\",new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7257c92",
   "metadata": {},
   "source": [
    "## week08\n",
    "## list练习和字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd68e711",
   "metadata": {},
   "source": [
    "### 上周回顾\n",
    "\n",
    ">* list列表一般方法/函数 的所有的使用场景及案例,方法如：\n",
    ">>1.append()\n",
    ">>2.pop(index)\\remove(values)\n",
    ">>3.count()\n",
    ">>4.index()\n",
    ">>5.extend()\n",
    ">>6.sort()\n",
    ">>......\n",
    ">*list高级方法 如：\n",
    ">>1.copy() 复制 不等于 赋值,复制可 等价于 切片[:]\n",
    ">>2.enu\n",
    ">*课本练习（课堂上）Don't panic==> on tap， 其中知识点：\n",
    ">>1.list的一些方法实现\n",
    ">>2.list的切片实现\n",
    ">>3.str => list 的方法 list(str)\n",
    ">>4.list =>的方法 ‘’.joint(list)\n",
    "\n",
    "### 本周内容\n",
    "### 先来做一些简单的练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eff4a4b8",
   "metadata": {},
   "source": [
    "### 1.vowels2.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "41d43e8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']# 这是元音(list)\n",
    "word = \"milliways\" #这是文本(str)\n",
    "# 需求：word里出现的vowels打印出来\n",
    "for letter in word: # letteer可以随便命名，不会影响我们的结果 一版情况下，item（内容）=>简写：\n",
    "    #(循环便利的第一个位置的元素注意名字不要和已经定义过的变量重复，\n",
    "    #for循环的第二个元素决定了我们循环的内容，循环的列表，\n",
    "    #每个1元素单独在一行打印\n",
    "    #作对象 for循环 先操作word)\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "f764cab9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['i', 'a']"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# append()经常用于新建列表\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0edb4168",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据结构练习1：购物车程序项目\n",
    "user_data = \"zengqiaoyi\"\n",
    "password_data = \"123456\"\n",
    "\n",
    "username = input(\"请输入您的账号：\")\n",
    "password = input(\"请输入您的密码：\")\n",
    "\n",
    "    if user_data == username:\n",
    "        if password_data == password:\n",
    "            print(欢迎进入购物车！)\n",
    "            else\n",
    "            print(用户或密码输入错误！)\n",
    "            break"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
